<?php

namespace app\components;


use app\entity\SysUserEntity;
use app\exception\ErrorCode;
use app\exception\OaException;
use yii\validators\Validator;

class UserNameValidator extends Validator
{

    public function validateAttribute($model, $attribute)
    {
        $value = $model->$attribute;


        if ($userId = $model->userId) {
            $e = SysUserEntity::find()->where("user_id!=:user_id and username=:username",[":user_id"=>$userId,":username"=>$value])->one();
            if ($e)   throw new OaException(ErrorCode::USERNAME_EXISTS);
        } else {
            $e = SysUserEntity::find()->where(" username!=:username",[":username"=>$value])->one();
            if ($e) throw new OaException(ErrorCode::USERNAME_EXISTS1);
        }




    }
}